package jimm;

import DrawControls.*;
import jimm.comm.*;
import jimm.comm.Icq;
import java.util.*;
import jimm.util.ResourceBundle;
import java.io.*;
import javax.microedition.lcdui.*;

import DrawControls.ListItem;
import DrawControls.VirtualList;
import DrawControls.TextList;
import DrawControls.VirtualListCommands;
import DrawControls.PopupList;
import DrawControls.PopupListCommands;

import jimm.comm.SearchAction;
import jimm.comm.Action;
import jimm.comm.XStatus;
import jimm.comm.UpdateContactListAction;



class GMList extends VirtualList implements VirtualListCommands
{
	public GM menuholder;
	public Object parent;
	
	static TextList messText;

	// items collection 
	private ListItem[] items;

	private int lastItemIndex = -1;

	public static final int CMD_BACK = 123456;

	private Displayable backScreen;
	public boolean autoBack;

	
	public GMList()
	{
		super(null);

		setVLCommands(this);
		JimmUI.setColorScheme(this);

		// #sijapp cond.if target is "MIDP2" | target is "SIEMENS2"#
		if(Options.getBoolean(Options.OPTION_CL_SMALL_FONT))
		setFontSize(VirtualList.SMALL_FONT);
		else
		setFontSize(VirtualList.MEDIUM_FONT);
		// #sijapp cond.else#
		setFontSize((imageList.getHeight() < 16) ? VirtualList.SMALL_FONT : VirtualList.MEDIUM_FONT);
		// #sijapp cond.end#

		this.setCursorMode(4);

		lastItemIndex = -1;

		leftCaption = ResourceBundle.getString("select");
		rightCaption = ResourceBundle.getString("back");

		setFullScreenMode(true);
		autoBack = false;

	}

	public void setRightCaption(String s)
	{
		rightCaption = s;
	}	

	public ListItem newListItem(int imageIndex, String txt, int value)
	{
		ListItem li = new ListItem();
		li.imageIndex = imageIndex;
		li.text = txt;
		li.color = Options.getSchemeColor(Options.CLRSCHHEME_TEXT);
		li.image = null;
		li.itemValue = value;
		return li;
	}

	public ListItem newListItem(Image image, String txt, int value)
	{
		ListItem li = newListItem(0, txt, value);
		li.image = image;
		return li;	
	}

	public void setImages(ImageList il)
	{
		setImageList(il);           
	}

	public ListItem getItem(int index)
	{
		if (index >= items.length)
			return null;

		return items[index];
	}

	public void setItemsCount(int count, boolean doEmpty)
	{
		if (doEmpty)
		{
			lastItemIndex = -1;
		}

		ListItem[] items1 = new ListItem[count];

		// move all elements from old array to the new
		for (int i = 0; i <= lastItemIndex; i++)
		{
			items1[i] = items[i];
		}

		items = null;
		System.gc();		

		// move pointer to new data array
		items = items1;
	}

	// add item to array
	public void addItem(int imageIndex, String txt, int value)
	{
		// if new item requres mem. BAD !!!!!
		if (lastItemIndex + 1 >= items.length)
		{
			setItemsCount(lastItemIndex + 2, false);
		}

		lastItemIndex++;

		items[lastItemIndex] = new ListItem();

		items[lastItemIndex].text = txt;
		items[lastItemIndex].imageIndex = imageIndex;
		items[lastItemIndex].color = Options.getSchemeColor(Options.CLRSCHHEME_TEXT);
		items[lastItemIndex].image = null;
		items[lastItemIndex].itemValue = value;

	}
	public void addItem(Image image, String txt, int value)
	{
		addItem(-1, txt, value);
		items[lastItemIndex].image = image;
	}
	
	// VirtualList command impl.
	public void onCursorMove(VirtualList sender)
	{
	}
	
	// VirtualList command impl.
	public boolean onKeyPress(VirtualList sender, int keyCode, int type)
	{
		if ((sender == this) && (type == VirtualList.KEY_PRESSED))
		{
			if (keyCode == Jimm.leftSoftKey) 
			{
				menuholder.events(parent, items[currItem].itemValue);
				return true;
			}
			else if (keyCode == Jimm.rightSoftKey || ((Jimm.is_phone_SE()) && (keyCode == -11)))	
			{
				if (autoBack)
					Jimm.display.setCurrent(backScreen);
				else
					menuholder.events(parent, CMD_BACK);
				
				return true;
			}
		}
		return JimmUI.execHotKey(null, keyCode, type);
	}
	
	// VirtualList command impl.
	public void onItemSelected(VirtualList sender)
	{
		if (sender == this)
		{
			menuholder.events(parent, items[currItem].itemValue);
		}
	}

	protected int getSize()
	{
		if (items == null)
		{
			return 0;
		}
		else
		{
			return items.length;
		}
	}
	  
	// returns messages history list item data
	protected void get(int index, ListItem item)
	{
		item.text = items[index].text;
		item.imageIndex = items[index].imageIndex;
		item.image = items[index].image;
		item.color = Options.getSchemeColor(Options.CLRSCHHEME_TEXT);
		item.itemValue = items[index].itemValue;
	}

	public void show()
	{
		backScreen = Jimm.display.getCurrent();
		Jimm.display.setCurrent(this);
	}
}

class GraphMenuBasic
{
	public GMList list;

	public void setImageList(ImageList il)
	{
		list.setImages(il);
	}

	public void setItems(ListItem[] li)
	{
	}

	public void setColorScheme()
	{
		if (list != null)
		{
			JimmUI.setColorScheme(list);
			if (AllSeeingEyeList.messText != null)
				JimmUI.setColorScheme(AllSeeingEyeList.messText);
		}
	}

//	public abstract void show();
	public void show()
	{
		list.show();
	}


}
class MenuMain extends GraphMenuBasic
{
	// MAIN MENU ITEMS
	public static final int MENU_CONNECT = 1;
	public static final int MENU_DISCONNECT = 2;
	public static final int MENU_LIST = 3;
	public static final int MENU_OPTIONS = 4;
	public static final int MENU_TRAFFIC = 5;
	public static final int MENU_KEYLOCK = 6;
	public static final int MENU_STATUS = 7;
	public static final int MENU_XSTATUS = 8;
	public static final int MENU_BALLOON = 20;
	public static final int MENU_GROUPS = 9;
	public static final int MENU_ABOUT = 10;
	public static final int MENU_MINIMIZE = 11;
	public static final int MENU_SOUND = 12;
	public static final int MENU_MYSELF = 13;
	public static final int MENU_CALL = 14;
	public static final int MENU_EYE = 15;
	public static final int MENU_EXIT = 16;
	public static final int MENU_PRIVACY_STATUS = 21;
//	public static final int MENU_CALL = 22;

	final public static ImageList imageListgg = ImageList.load("/icons.png");
                                                          

		String[] menuText = {"keylock_enable", "set_status", "set_xstatus", "", "", "contact_list", "options_lng", "manage_contact_list",
			                 "myself", "traffic_lng", "all_seeing_eye", "calls", "about", "minimize", "exit"};

		int[] menuIconIndex = {0, -1, -1, -1, -1, 1, 3, 1, 9, 24, 33, -1, 4, 27, 28 };
		int[] menuValues = {MENU_KEYLOCK, MENU_STATUS, MENU_XSTATUS, MENU_PRIVACY_STATUS, MENU_SOUND, MENU_LIST, MENU_OPTIONS, MENU_GROUPS,MENU_MYSELF, MENU_TRAFFIC, MENU_EYE, MENU_CALL, MENU_ABOUT, MENU_MINIMIZE, MENU_EXIT};



	// #sijapp cond.if target isnot "DEFAULT" #	
	static private String getSoundValue(boolean value)
	{
		return ResourceBundle.getString(value ? "#sound_on" : "#sound_off");
	}
	static private Image getSoundPicture(boolean value)
	{
		return (value ? ContactList.menuIcons.elementAt(16) : ContactList.menuIcons.elementAt(15));
	}
	// #sijapp cond.end#	

	public static final int PR_VISIBLE_FOR_ALL = 1;
	public static final int PR_INVISIBLE_FOR_ALL = 2;
	public static final int PR_VISIBLE_ONLY_FOR_VL = 3;
	public static final int PR_VISIBLE_FOR_ALL_EXCEPT_IL = 4;
	public static final int PR_VISIBLE_FOR_CL = 5;


	public static final int[] pr_sts = {0,4,1,2,3};
	String[] pr_sts_txt = {"pr_status1", "pr_status5", "pr_status2", "pr_status3", "pr_status4"};



	MenuMain()
	{
/*
		String caption = ResourceBundle.getString("menu");
		list = new GMList();
		list.setCaption(caption);
		list.parent = (Object)this;
*/
		rebuild();
	}

	public void rebuild()
	{
		if (list == null)
		{
			String caption = ResourceBundle.getString("menu");
			list = new GMList();
			list.setCaption(caption);
			list.parent = (Object)this;
		
//			list.leftCaption = ResourceBundle.getString("select");
			list.rightCaption = ""; //ResourceBundle.getString("back");
		}

		list.lock();
//		if (list.getSize() != 0) list.setCurrentItem(list.getSize() - 1);
		list.setImageList(ContactList.menuIcons);
		list.setItemsCount(11, true);
		
		int itmCnt = 1;
		if (Icq.isNotConnected())
		{
			list.addItem(17, ResourceBundle.getString("connect"), MENU_CONNECT);      		
			if (list.getCurrIndex() == 1)
			{
				list.setCurrentItem(0);
			}
		}
		else
		{
			list.addItem(menuIconIndex[0], ResourceBundle.getString(menuText[0]), menuValues[0]);
			list.addItem(18, ResourceBundle.getString("disconnect"), MENU_DISCONNECT);
		}


		for (int i = 1; i < 3; i++)
			list.addItem(i == 1? Jimm.getStatusImage() : 
					                 Jimm.getXStatusImage(), ResourceBundle.getString(menuText[i]), 
									                              menuValues[i]
							);

		int sts_int = Options.getInt(Options.OPTION_PRIVACY_STATUS)-1;
		Image pricon = ContactList.privateStatuses.elementAt(pr_sts[sts_int]);
		list.addItem(pricon, ResourceBundle.getString(pr_sts_txt[sts_int]), MENU_PRIVACY_STATUS);

		//#sijapp cond.if target isnot "DEFAULT"#
		boolean isSilent = Options.getBoolean(Options.OPTION_SILENT_MODE);
		list.addItem(getSoundPicture(isSilent), getSoundValue(isSilent), MENU_SOUND);
		//#sijapp cond.end# 


	
		for (int i = 5; i < menuText.length; i++)
		{
			if ((i == 5) && Icq.isConnected())
			{
				continue;
			}
			if ((i == 7 || i == 8/* || i == 12*/) && Icq.isNotConnected())
			{
				continue;
			}
			
			// #sijapp cond.if modules_TRAFFIC isnot "true" #
			if (i == 9)
				continue;
			// #sijapp cond.end#
			if (i == 10 && !Options.getBoolean(Options.OPTION_ALLSEEINGEYE))
				continue;

			if (i == 11)
			{
				list.addItem(ContactList.micons2.elementAt(0)/*XStatus.imageList.elementAt(14)*/, ResourceBundle.getString(menuText[i]), menuValues[i]);
				continue;
			}

			list.addItem(menuIconIndex[i], ResourceBundle.getString(menuText[i]), menuValues[i]);
		}

		list.unlock();
	}

	public void refresh()
	{
		rebuild();
/*
		if (Icq.isNotConnected())
		{
			list.setItem(0, list.newListItem(17, ResourceBundle.getString("connect"), MENU_CONNECT));
		}
		else
		{
			list.setItem(0, list.newListItem(18, ResourceBundle.getString("disconnect"), MENU_DISCONNECT));
		}

		list.setItem(1, list.newListItem(MainMenu.getStatusImage(), ResourceBundle.getString(menuText[0]), menuValues[0]));

		list.setItem(2, list.newListItem(MainMenu.getXStatusImage(), ResourceBundle.getString(menuText[1]), menuValues[1]));

		boolean isSilent = Options.getBoolean(Options.OPTION_SILENT_MODE);
		list.setItem(3, list.newListItem(getSoundPicture(isSilent), getSoundValue(isSilent), MENU_SOUND));

		if (Icq.isConnected())
		{
		}
*/

	}
}

class MenuStatus extends GraphMenuBasic
{
	public static final String[] texts = { "status_online", "status_chat", "status_evil", "status_depression", "status_home", "status_work", "status_lunch", "status_away", "status_na", "status_occupied", "status_dnd", "status_invisible", "status_invis_all" };
	public static final int[] imInd = { 7, 1, 8, 9, 10, 11, 12, 0, 4, 5, 2, 3, 13 };

        MenuStatus()
	{
		if (list == null)
		{
			list = new GMList();
			list.setCaption(ResourceBundle.getString("set_status"));
			list.parent = (Object)this;

		}

		list.lock();

		list.setImageList(ContactList.imageList);

		list.setItemsCount(texts.length, true);

		for (int i = 0; i < texts.length; i++)
		{
			list.addItem(imInd[i], ResourceBundle.getString(texts[i]), i);
		}

		list.unlock();


	}
	
}

class MenuOptions extends GraphMenuBasic
{
	
// Static constants for menu actios
    public static final int OPTIONS_ACCOUNT      =  0;
    public static final int OPTIONS_NETWORK      =  1;
    // #sijapp cond.if modules_PROXY is "true"#       
	public static final int OPTIONS_PROXY = 2;
    // #sijapp cond.end#
	public static final int OPTIONS_INTERFACE = 3;
	public static final int OPTIONS_COLOR_SCHEME = 4;
	public static final int OPTIONS_HOTKEYS = 5;
	public static final int OPTIONS_SIGNALING = 6;
    // #sijapp cond.if modules_TRAFFIC is "true"#
	public static final int OPTIONS_TRAFFIC = 7;
    // #sijapp cond.end#

	public static final int OPTIONS_TIMEZONE = 8;
	public static final int OPTIONS_MISC = 9;
	public static final int OPTIONS_ANTISPAM = 10;
    
	public static final int OPTIONS_LISTS = 11;
	public static final int OPTIONS_AUTOAWAY = 12;

	// Exit has to be biggest element cause it also marks the size
	public static final int MENU_EXIT = 13;




	MenuOptions()
	{
		if (list == null)
		{
			String caption = ResourceBundle.getString("options_lng");
			list = new GMList();
			list.setCaption(caption);
			list.parent = (Object)this;
		}

		list.lock();

		int menuCount = 9;
		// #sijapp cond.if modules_PROXY is "true"#
		menuCount++;
		// #sijapp cond.end#
		// #sijapp cond.if modules_TRAFFIC is "true"#
		menuCount++;
		// #sijapp cond.end#
		list.setItemsCount(menuCount, true);

		list.setImageList(ContactList.menuIcons);

		list.addItem(12, ResourceBundle.getString("options_account"), OPTIONS_ACCOUNT);
		list.addItem(13, ResourceBundle.getString("options_network"), OPTIONS_NETWORK);

		// #sijapp cond.if modules_PROXY is "true"#
		if (Options.getInt(Options.OPTION_CONN_TYPE) == Options.CONN_TYPE_PROXY)
			list.addItem(21, ResourceBundle.getString("proxy"), OPTIONS_PROXY);
		// #sijapp cond.end#
		list.addItem(14, ResourceBundle.getString("options_interface"), OPTIONS_INTERFACE);
		list.addItem(32, ResourceBundle.getString("color_scheme"), OPTIONS_COLOR_SCHEME);

		list.addItem(14, ResourceBundle.getString("options_lists"), OPTIONS_LISTS);

		list.addItem(22, ResourceBundle.getString("options_hotkeys"), OPTIONS_HOTKEYS);
		list.addItem(15, ResourceBundle.getString("options_signaling"), OPTIONS_SIGNALING);

		list.addItem(ContactList.micons2.elementAt(1), ResourceBundle.getString("auto_away"), OPTIONS_AUTOAWAY);
		
		// #sijapp cond.if modules_TRAFFIC is "true"#
		list.addItem(25, ResourceBundle.getString("options_cost"), OPTIONS_TRAFFIC);
		// #sijapp cond.end#
		list.addItem(26, ResourceBundle.getString("time_zone"), OPTIONS_TIMEZONE);
		list.addItem(0, ResourceBundle.getString("antispam"), OPTIONS_ANTISPAM);
		list.addItem(10, ResourceBundle.getString("misc"), OPTIONS_MISC);

		list.unlock();
	}
}

class MenuGroups extends GraphMenuBasic implements CommandListener
{
	public static final int STATUS_NONE         = 0;
	public static final int STATUS_ADD_CONTACT  = 1;
	public static final int STATUS_ADD_GROUP    = 2;
	public static final int STATUS_RENAME_GROUP = 3;
	public static int status = STATUS_NONE;

	public static final int TAG_EXIT = 1;
	public static final int TAG_RENAME_GROUPS = 2;
	public static final int TAG_DELETE_GROUPS = 3;
	public static final int TAG_CL = 4;

	public static Form textBoxForm;
	public static TextField	uinTextField;

	static public int[] groupIds;


	//#sijapp cond.if target is "MIDP2"#
	private static Command backCommand = new Command(ResourceBundle.getString("back"), Jimm.is_smart_SE() ? Command.CANCEL : Command.BACK, 1);
	//#sijapp cond.else#
	private static Command backCommand = new Command(ResourceBundle.getString("back"), Command.BACK, 1);
	//#sijapp cond.end#

	/* Send command */
	private static Command sendCommand = new Command(ResourceBundle.getString("send"), Command.OK, 1);

	/* Select command */
	private static Command selectCommand = new Command(ResourceBundle.getString("select"), Command.OK, 1);

	// #sijapp cond.if target is "MOTOROLA" | target is "SIEMENS2"#
	private static Command exitCommand = new Command(ResourceBundle.getString("exit_button"), Command.EXIT, 1);
	//#sijapp cond.end#

	
	private static MenuGroups _this;



	MenuGroups()
	{
		if (list == null)
		{

			String caption = ResourceBundle.getString("manage_contact_list");

			list = new GMList();
			list.setCaption(caption);
			list.parent = (Object)this;
		}

		list.lock();

		_this = this;

		list.setItemsCount(4, true);
		list.setImageList(ContactList.menuIcons);
		

		list.addItem(2,ResourceBundle.getString("search_user"), 0);
		list.addItem(29,ResourceBundle.getString("add_group"), 1);
		list.addItem(11,ResourceBundle.getString("rename_group"), 2);
		list.addItem(6,ResourceBundle.getString("del_group"), 3);

		list.unlock();

	}

	/* Show form for adding user */
	static public void showTextBoxForm(String caption, String label, String text, int fieldType)
	{
		textBoxForm = new Form(ResourceBundle.getString(caption));
		uinTextField = new TextField(ResourceBundle.getString(label), text, 16, fieldType);
		textBoxForm.append(uinTextField);
		
		textBoxForm.addCommand(sendCommand);
		textBoxForm.addCommand(backCommand);
		textBoxForm.setCommandListener(_this);
		Jimm.display.setCurrent(textBoxForm);
	}


	public void commandAction(Command c, Displayable d)
	{
		if ((c == sendCommand) && (d == textBoxForm))
		{
			Action act = null;

			switch (status)
			{
			case STATUS_ADD_GROUP:
				ContactListGroupItem newGroup = new ContactListGroupItem(uinTextField.getString());
				act = new UpdateContactListAction(newGroup, UpdateContactListAction.ACTION_ADD);
				break;
				
			case STATUS_RENAME_GROUP:
				ContactListGroupItem group = ContactList.getGroupById(groupIds[JimmUI.getLastSelIndex()]);
				group.setName(uinTextField.getString());
				ContactList.safeSave();
				act = new UpdateContactListAction(group, UpdateContactListAction.ACTION_RENAME);
				break;
				
			case STATUS_ADD_CONTACT:
				Search search = new Search(true);
				String data[] = new String[Search.LAST_INDEX];
				data[Search.UIN] = uinTextField.getString();
				act = new SearchAction(search, data, SearchAction.CALLED_BY_ADDUSER);
				break;
			}
			
			status = STATUS_NONE;
			
			try
			{
				Icq.requestAction(act);
			}
			catch (JimmException e)
			{
				JimmException.handleException(e);
				if (e.isCritical()) return;
			}
			// Start timer
			SplashCanvas.addTimerTask("wait", act, false);
		}
		else if (c == MenuGroups.backCommand)
		{
			if (d == textBoxForm) Jimm.gm.showGroupsMenu(); //MainMenu.activate();
		}
		
	}

}

          
class MenuPrivacyStatus extends GraphMenuBasic
{
	MenuPrivacyStatus()
	{
		if (list == null)
		{

			String caption = ResourceBundle.getString("priv_stat");

			list = new GMList();
			list.setCaption(caption);
			list.parent = (Object)this;
		}

		list.lock();

		list.setItemsCount(5, true);
		list.setImageList(ContactList.privateStatuses);
		

		list.addItem(0,ResourceBundle.getString("pr_status1"), Options.PR_VISIBLE_FOR_ALL);
		list.addItem(1,ResourceBundle.getString("pr_status2"), Options.PR_VISIBLE_ONLY_FOR_VL);
		list.addItem(2,ResourceBundle.getString("pr_status3"), Options.PR_VISIBLE_FOR_ALL_EXCEPT_IL);
		list.addItem(3,ResourceBundle.getString("pr_status4"), Options.PR_VISIBLE_FOR_CL);
		list.addItem(4,ResourceBundle.getString("pr_status5"), Options.PR_INVISIBLE_FOR_ALL);

		list.setCurrentItem(MenuMain.pr_sts[Options.getInt(Options.OPTION_PRIVACY_STATUS)-1]);
		
		list.unlock();

	}
}
        

class MenuContactItem  extends GraphMenuBasic // implements CommandListener
{

	private ContactListContactItem item;
	public Displayable backScreen;

	MenuContactItem()
	{
		backScreen = null;
	}

	public void init(ContactListContactItem item)
	{

		boolean showAuthItem =  item.getBooleanValue(ContactListContactItem.CONTACTITEM_NO_AUTH);
		long status = item.getIntValue(ContactListContactItem.CONTACTITEM_STATUS);

		String caption = ResourceBundle.getString("manage_contact_list");

		list = new GMList();
		list.setCaption(caption);
		list.parent = (Object)this;

		list.lock();

		list.setItemsCount(10, true);
		list.setImageList(ContactList.menuIcons);
	
		this.item = item;
		

		list.addItem(ContactList.imageList.elementAt(14), ResourceBundle.getString("send_message"), ContactListContactItem.USER_MENU_MESSAGE);

        if (showAuthItem)
            list.addItem(ContactList.imageList.elementAt(16), ResourceBundle.getString("requauth"), ContactListContactItem.USER_MENU_REQU_AUTH);




				
	if (item.getBooleanValue(ContactListContactItem.CONTACTITEM_IS_TEMP))// && !getBooleanValue(ContactListContactItem.CONTACTITEM_NO_AUTH)) 
	{
		list.addItem(5, ResourceBundle.getString("add_user"), ContactListContactItem.USER_MENU_ADD_TO_CL);
	}
	





       
        if (JimmUI.getClipBoardText() != null)
        	list.addItem(20, ResourceBundle.getString("quote"), ContactListContactItem.USER_MENU_QUOTA);
        
/*
        // #sijapp cond.if target is "MIDP2" | target is "MOTOROLA" | target is "SIEMENS2"#
        // #sijapp cond.if modules_FILES is "true"#
		if ((status != ContactList.STATUS_OFFLINE) && (item.getIntValue(ContactListContactItem.CONTACTITEM_ICQ_PROT) >= 8)
			&& (item.getIntValue(ContactListContactItem.CONTACTITEM_CLIENT) != Util.CLI_JIMM))
		{
			list.addItem(ContactList.menuIcons.elementAt(8), ResourceBundle.getString("ft_name"), USER_MENU_FILE_TRANS);
			// #sijapp cond.if target isnot "MOTOROLA"#
			// #sijapp cond.if target is "MIDP2"#
			if (!Jimm.is_phone_SE())
			// #sijapp cond.end#
			eventList[menuList.append(ResourceBundle.getString("ft_cam"), ContactList.menuIcons.elementAt(23))] = USER_MENU_CAM_TRANS;
			// #sijapp cond.end#
		}
		// #sijapp cond.end#
		// #sijapp cond.end#
*/
//        if ((status != ContactList.STATUS_ONLINE) && (status != ContactList.STATUS_OFFLINE) &&
//				(status != ContactList.STATUS_INVISIBLE))
//       		list.addItem(item.getImageIndex(), ResourceBundle.getString("reqstatmsg"), ContactListContactItem.USER_MENU_STATUS_MESSAGE);

        if ((item.getXStatus().getStatusImage() != null) && (status != ContactList.STATUS_OFFLINE))
        	list.addItem(item.getXStatus().getStatusImage(), ResourceBundle.getString("xtraz_msg"), ContactListContactItem.USER_MENU_XTRAZ_MESSAGE);

        list.addItem(9, ResourceBundle.getString("info"), ContactListContactItem.USER_MENU_USER_INFO);

	list.addItem(19, ResourceBundle.getString("copy_uin"), ContactListContactItem.USER_MENU_COPY_UIN);

        if (status != ContactList.STATUS_OFFLINE)
		list.addItem(10, ResourceBundle.getString("dc_info"), ContactListContactItem.USER_MENU_LOCAL_INFO);

        // #sijapp cond.if modules_HISTORY is "true" #
        list.addItem(7, ResourceBundle.getString("history"), ContactListContactItem.USER_MENU_HISTORY);
        // #sijapp cond.end#

		list.addItem(7, ResourceBundle.getString("statistics"), ContactListContactItem.USER_MENU_STATISTICS);
        
/*
        if ((ContactList.getGroupItems().length > 1) && (!item.getBooleanValue(CONTACTITEM_NO_AUTH)) &&	(!item.getBooleanValue(CONTACTITEM_IS_TEMP)))
			eventList[menuList.append(ResourceBundle.getString("move_to_group"), ContactList.menuIcons.elementAt(??))] = USER_MENU_MOVE;
*/

	boolean btmp = Options.xstatusUins.containsKey(item.getStringValue(ContactListContactItem.CONTACTITEM_UIN));
	list.addItem(ContactList.menuIcons.elementAt(4), ResourceBundle.getString(btmp ? "dont_allow_read_xstatus" : "allow_read_xstatus"), btmp ? ContactListContactItem.DONT_ALLOW_READ_XSTATUS : ContactListContactItem.ALLOW_READ_XSTATUS);

		
        // Privacy Lists
	list.addItem(31, ResourceBundle.getString("server_lists"), ContactListContactItem.USER_MENU_LIST_OPERATION);
        list.addItem(30, ResourceBundle.getString("remove_me"), ContactListContactItem.USER_MENU_REMOVE_ME);
	list.addItem(11, ResourceBundle.getString("rename"), ContactListContactItem.USER_MENU_RENAME);
        list.addItem(6, ResourceBundle.getString("remove"), ContactListContactItem.USER_MENU_USER_REMOVE);

	list.unlock();
	}


	public void commandAction(int acti)
	{
		if (item == null)
		{
		} 
		else
		{
			item.itemWasSelected(acti);
		}
	}
}



public class GM
{
	private MenuMain menumain;
	private MenuStatus menustatus;
	private MenuGroups menugroups;
	private MenuOptions menuoptions;
	private MenuContactItem menucontact;
	private MenuPrivacyStatus menuprstatus;

	public boolean NeedToRefreshMainMenu = false;

	public void eventsMenuMain(int itemVal)
	{
	      	boolean anyway = false;

		switch (itemVal)
		{
			case MenuMain.MENU_PRIVACY_STATUS:
				showPrivacyStatus();
				break;

			case MenuMain.MENU_CONNECT:
				Icq.reconnect_attempts = Options.getInt(Options.OPTION_RECONNECT_NUMBER);
				ContactList.beforeConnect();
				Icq.connect();
				freeMainMenu();
				break;

			case MenuMain.MENU_DISCONNECT:
				Icq.disconnect();
				Thread.yield();

				refreshMainMenu();
				menumain.show();
				break;

			// back pressed
			case GMList.CMD_BACK:
				if (!Icq.isConnected())
					break;

			case MenuMain.MENU_LIST:
				ContactList.activate();
				break;

			case MenuMain.MENU_KEYLOCK:
				SplashCanvas.lock();
				break;

			case MenuMain.MENU_STATUS:
				showStatusMenu();
				freeMainMenu();
				break;


			case MenuMain.MENU_XSTATUS:
                                XTrazTable.selectXtraz(menumain.list); //menumain.list);
				break;

			case MenuMain.MENU_GROUPS:
				menugroups.show();
				freeMainMenu();
				break;

			case MenuMain.MENU_OPTIONS:
				Options.editOptions();
				freeMainMenu();
				break;


			case MenuMain.MENU_TRAFFIC:
				Traffic.trafficScreen.activate();
				freeMainMenu();
				break;

			case MenuMain.MENU_EYE:
				AllSeeingEye.showHistoryList("","", true);
				freeMainMenu();
				break;

			case MenuMain.MENU_ABOUT:
				JimmUI.about(menumain.list);
				freeMainMenu();
				break;
// #sijapp cond.if target isnot "MOTOROLA" #
/*
			case MenuMain.MENU_MINIMIZE:
			{
				Jimm.setMinimized(true);
			}
*/
//#sijapp cond.end#				

			// #sijapp cond.if target isnot "DEFAULT" #
			case MenuMain.MENU_SOUND:
				boolean soundValue = ContactList.changeSoundMode( Icq.isConnected() );
				menumain.refresh();

			//	boolean soundValue = ContactList.changeSoundMode( Icq.isConnected() );
			//	MainMenu.list.set(selectedIndex, getSoundValue(soundValue), getSoundPicture(soundValue));
			//	MainMenu.rebuild();
				break;
				//#sijapp cond.end#				

			case MenuMain.MENU_MYSELF:
				JimmUI.requiestUserInfo(Options.getString(Options.OPTION_UIN), "");
				break;

			case MenuMain.MENU_MINIMIZE:


				Jimm.setMinimized(true);  
				break;
			case MenuMain.MENU_CALL:

				Jimm.phb.promptAndSend(Jimm.gm.getMainMenuList()); //"+79851319925");
				break;


/*
				System.gc();
				System.out.print("Free memory=");
				System.out.println((Runtime.getRuntime().totalMemory()-Runtime.getRuntime().freeMemory())/1024);
				
				if (menu1 == null)
				{
					System.out.println("creating menu1...");
					menu1 = new MenuStatus();
					menu1.list.menuholder = this;
					menu1.show();
				}
				else
				{
					System.out.println("freeing menu1...");
					menu1 = null;
				}


				System.gc();
				System.out.print("Free memory=");
				System.out.println((Runtime.getRuntime().totalMemory()-Runtime.getRuntime().freeMemory())/1024);
*/	
	
				

	

/*
			{
				//Jimm.setMinimized(true);
				try 
				{
					anyway = true;
					Jimm.jimm.platformRequest("http://anton.kochetkov.ru/download/jimm.de.jad");
				}
				catch(Exception e)
				{
				}
			}

*/
			case MenuMain.MENU_EXIT:

			/*
		if (!anyway && ContactList.getUnreadMessCount() > 0)
		{
			JimmUI.messageBox(ResourceBundle.getString("attention"), ResourceBundle.getString("have_unread_mess"), JimmUI.MESBOX_YESNO, this, 1);
		}
		else      */
		{         
			Icq.disconnect();
   			try
			{
				Thread.sleep(500);
			} catch (Exception e1)
			{
			}
			try
			{
				Jimm.jimm.destroyApp(true);
			}
			catch (Exception e)
			{ 
			}
		}
				break;
		} // switch

	} // proc

	public static final	long[] sts = {ContactList.STATUS_ONLINE, ContactList.STATUS_CHAT, ContactList.STATUS_EVIL, ContactList.STATUS_DEPRESSION,
				ContactList.STATUS_HOME, ContactList.STATUS_WORK, ContactList.STATUS_LUNCH,
				ContactList.STATUS_AWAY, ContactList.STATUS_NA, ContactList.STATUS_OCCUPIED,
				ContactList.STATUS_DND, ContactList.STATUS_INVISIBLE, ContactList.STATUS_INVIS_ALL};

	public void eventsMenuStatus(int itemVal)
	{
		if (itemVal == GMList.CMD_BACK)
		{
			showMainMenu();
			return; 		
		}

		Options.setLong(Options.OPTION_ONLINE_STATUS, sts[itemVal]);
		refreshMainMenu();
		showMainMenu();
		Options.safe_save();

		if (Icq.isConnected())
		{
			try
			{
				Icq.setOnlineStatus((int)sts[itemVal]);
			}
			catch (JimmException e)
			{
				JimmException.handleException(e);
				if (e.isCritical()) return;
			}
		}

	}

	public void eventsMenuGroups(int itemVal)
	{
     		switch (itemVal)
		{
		case GMList.CMD_BACK:
			showMainMenu();
			break;
		case 0:
			Search searchf = new Search(false);
			searchf.getSearchForm().activate(Search.SearchForm.ACTIV_JUST_SHOW);
			break;
			
		case 1:
			menugroups.status = MenuGroups.STATUS_ADD_GROUP;
			menugroups.showTextBoxForm("add_group", "group_name", null, TextField.ANY);
			break;
			
		case 2: 
			menugroups.status = MenuGroups.STATUS_RENAME_GROUP;
			menugroups.groupIds = JimmUI.showGroupSelector("rename_group", MenuGroups.TAG_RENAME_GROUPS, menugroups, JimmUI.SHS_TYPE_ALL, -1); 
			break;
			
		case 3: 
			menugroups.groupIds = JimmUI.showGroupSelector("del_group", MenuGroups.TAG_DELETE_GROUPS, menugroups, JimmUI.SHS_TYPE_EMPTY, -1);
			break;
		}
		

	}

	public void eventsMenuOptions(int itemVal)
	{
//		System.out.println("in eventsMenuOptions");

		if (itemVal == GMList.CMD_BACK)
		{
			showMainMenu();
			return;
		}
		Options.sndVar = itemVal;
		Options.optionsForm.commandAction(List.SELECT_COMMAND, menugroups.list);
	}

	public void eventsMenuContactItem(int itemVal)
	{
		if (itemVal == GMList.CMD_BACK)
		{
			if (menucontact.backScreen == null)
				ContactList.activate();
			else
			{
				Jimm.display.setCurrent(menucontact.backScreen);
			}
				
				
		}
		else

		menucontact.commandAction(itemVal);
	}


	public void eventsMenuPrivacyStatus(int itemVal)
	{
		if (itemVal == GMList.CMD_BACK)
		{
			menumain.show();
			return;
		}

		if (itemVal > 5)
			return;

		Options.setInt(Options.OPTION_PRIVACY_STATUS, itemVal);
		Options.safe_save();

		try
		{
			Icq.setOnlineStatus(Icq.getCurrentStatus());
		}
		catch (Exception e) {}
		
		menumain.refresh();
		menumain.show();

	}



	public void events(Object sender, int itemVal)
	{
		if (sender == menumain)
			eventsMenuMain(itemVal);
		else if ((sender == menustatus))
			eventsMenuStatus(itemVal);
		else if (sender == menugroups)
			eventsMenuGroups(itemVal);
		else if (sender == menuoptions)
			eventsMenuOptions(itemVal);
		else if (sender == menucontact)
			eventsMenuContactItem(itemVal);
		else if (sender == menuprstatus)
			eventsMenuPrivacyStatus(itemVal);
			
			



	}
	
	GM()
	{
		menumain = new MenuMain();
		menumain.list.menuholder = this;

		menugroups = new MenuGroups();
		menugroups.list.menuholder = this;

	}

	public void showMainMenu()
	{
		if (menumain == null)
		{	
			menumain = new MenuMain();
			menumain.list.menuholder = this;
		}
		else
		{
			refreshMainMenu();
		}
		JimmUI.setColorScheme(menumain.list);
		menumain.show();
	}

	public void freeMainMenu()
	{
/*
		if (Options.getBoolean(Options.OPTION_KEEP_MEMORY))
		{
			System.out.println("deleting main menu...");
			menumain = null;
		}
*/
	}

	public GMList getMainMenuList()
	{
		return menumain.list;
	}

	public void refreshMainMenu()
	{
		if (Options.getBoolean(Options.OPTION_KEEP_MEMORY))
		{
/*
			System.out.println("deleting status menu...");
			menustatus = null;

			System.out.println("deleting options menu...");
			menuoptions = null;

			System.out.println("deleting groups menu...");
			menugroups = null;
			
			System.out.println("deleting prstatus menu...");
			menuprstatus = null;
*/

		}

		if (menumain == null)
			return;

		menumain.refresh();
	}

	public void showStatusMenu()
	{
		if (menustatus == null)
		{
			menustatus = new MenuStatus();
			menustatus.list.menuholder = this;
		}

	 	long currs = Options.getLong(Options.OPTION_ONLINE_STATUS);
		for (int i = 0; i < sts.length; i++)
		{
			if (sts[i] == currs)
			{	
				menustatus.list.setCurrentItem(i);
				break;
			}
		}
		JimmUI.setColorScheme(menustatus.list);
		menustatus.show();
	}

	public void showOptionsMenu()
	{
		if (menuoptions == null)
		{
			menuoptions = new MenuOptions();
			menuoptions.list.menuholder = this;
		}

		menuoptions.list.setCurrentItem(0);
		JimmUI.setColorScheme(menuoptions.list);
		menuoptions.show();
	}
	
	public void showGroupsMenu()
	{

		if (menugroups == null)
		{
			menugroups = new MenuGroups();
			menugroups.list.menuholder = this;
		}

		menugroups.list.setCurrentItem(0);
		JimmUI.setColorScheme(menugroups.list);
		menugroups.show();
	}

	public GMList getOptionsList()
	{
//		if (menuoptions == null)
//			return null;

		return menuoptions.list;
	}
/*	
	public void showMenuContactItem(boolean auth, ContactListContactItem itm)
	{
		//menucontact = new MenuContactItem(); //(auth, itm);
		menucontact.show();
	}
*/
	public GMList getContactItemList()
	{
		if (menucontact == null)
			return null;

		return menucontact.list;
	}
	public void initContactMenu(ContactListContactItem item)
	{
		menucontact = new MenuContactItem();
		menucontact.init(item);

		if (menucontact.list != null)
			menucontact.list.menuholder = this;

		menucontact.list.setCaption(item.getStringValue(ContactListContactItem.CONTACTITEM_NAME));
		menucontact.show();
	}
	public void initContactMenu(ContactListContactItem item, Displayable back)
	{
		
		initContactMenu(item);
		menucontact.backScreen = back;
	}

	public void setContactMenuTitle(String title)
	{
		if (menucontact != null && menucontact.list != null)
		{
			menucontact.list.setCaption(title);
		}
	}
	                
	public void showPrivacyStatus()
	{
		if (menuprstatus == null)
		{
			menuprstatus = new MenuPrivacyStatus();
			menuprstatus.list.menuholder = this;
		}

		menuprstatus.show();
	}                 
	public void setMainMenuRightCaption(String str)
	{
		try
		{
			getMainMenuList().rightCaption = str;
		}
		catch (Exception e) {};
	}

}
